import ACTIONS from "./actions";

const evaluate = (state, operation) => {
    let last_operand = parseFloat(state.last_operand), cur_operand = parseFloat(state.cur_operand);
    console.log(last_operand, operation, cur_operand);
    
    let res = cur_operand;
    if (operation === "+") res = last_operand + cur_operand;
    else if (operation === "-") res = last_operand - cur_operand;
    else if (operation === "×") res = last_operand * cur_operand;
    else if (operation === "÷") res = last_operand / cur_operand;
    else if (operation === "x²" && state.cur_operand.length) res = cur_operand * cur_operand;
    else if (operation === "x²" && state.last_operand.length) res = last_operand * last_operand;
    else if (operation === "+/-" && state.cur_operand.length) res = -cur_operand;
    else if (operation === "+/-" && state.last_operand.length) res = -last_operand;
    
    return res.toString();
}

const reducer = (state = {
    last_operand: "",
    cur_operand: "",
    operation: "",
    rewrite: false
}, action) => {
    switch (action.type) {
        case ACTIONS.ADD_DIGIT:
            if (state.rewrite) return {
                last_operand: "",
                cur_operand: action.value,
                operation: "",
                rewrite: false
            };
            // console.log(state.cur_operand)
            if (state.cur_operand === "0" && action.value === "0") return state;
            if (state.cur_operand === "0" && action.value !== ".") return {
                ...state,
                cur_operand : action.value,
            }
            if (action.value === "." && state.cur_operand.includes(".")) return state;
            if (action.value === "." && state.cur_operand === "")  return {
                ...state,
                cur_operand: "0" + action.value,
            }
            return {
                ...state,
                cur_operand : state.cur_operand + action.value,
            }
        case ACTIONS.SUB_DIGIT:
            if (state.cur_operand.length !== 0) return {
                ...state,
                cur_operand: state.cur_operand.slice(0, -1),
            }
            return state;
        case ACTIONS.OPERATION:
            console.log(action.operation);

            if (state.cur_operand.length === 0 && state.last_operand === 0) return state;
            if (state.last_operand.length === 0) {
                if (action.operation === "x²" || action.operation === "+/-") {
                    return {
                        ...state,
                        last_operand: evaluate(state, action.operation),
                        cur_operand: "",
                    }
                }
                
                return {
                    ...state,
                    cur_operand: "",
                    operation: action.operation,
                    last_operand: state.cur_operand,
                }
            } 

            if (state.cur_operand.length === 0) {
                if (action.operation === "x²" || action.operation === "+/-") {
                    return {
                        ...state,
                        last_operand: evaluate(state, action.operation),
                    }
                }
                return {
                    ...state,
                    operation: action.operation,
                    rewrite: false
                }
            }

            if (action.operation === "x²" || action.operation === "+/-") {
                return {
                    ...state,
                    cur_operand: evaluate(state, action.operation)
                }
            }

            let res = evaluate(state, state.operation);

            return {
                last_operand: res,
                cur_operand: "",
                operation: action.operation,
                rewrite: false
            };
        case ACTIONS.CLEAR:
            return {
                last_operand: "",
                cur_operand: "",
                operation: "",
                rewrite: false
            }
        case ACTIONS.EVALUATE:
            if (state.cur_operand.length === 0 && state.last_operand === 0) return state;
            let result = evaluate(state, state.operation);
            return {
                last_operand: result,
                cur_operand: "",
                operation: action.operation,
                rewrite: true
            };
        default: 
            return state;
    }
}

export default reducer;